package com.whz.generic.api;

import com.google.common.collect.Maps;
import com.whz.generic.api.exception.CommonBusinessException;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.util.Map;

/**
 * @author whz
 */
@Data
public class ApiResponseWrapper<T> {

    public static final String ORIGIN = "origin";
    /**
     * 接口消息头
     */
    Map<String, String> header = Maps.newHashMapWithExpectedSize(1);
    /**
     * 接口消息状态
     */
    ApiStatus status;
    /**
     * 接口数据实体
     */
    T payLoad;
    /**
     * 接口错误信息
     */
    String error;
    /**
     * 接口错误码,应该自定义
     */
    Integer code = HttpStatus.OK.value();

    public static <T> ApiResponseWrapper<T> success(T payLoad) {
        ApiResponseWrapper<T> response = new ApiResponseWrapper<T>();
        response.setStatus(ApiStatus.SUCCESS);
        response.setCode(HttpStatus.OK.value());
        response.setPayLoad(payLoad);
        response.setError("");
        return response;
    }

    public static ApiResponseWrapper<String> fail(String error) {
        return fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), error);
    }

    /**
     * 只是为了适应泛型
     *
     * @param error 错误消息
     * @param <T>   任意类型
     * @return 返回的response
     */
    public static <T> ApiResponseWrapper<T> whenFail(String error) {
        ApiResponseWrapper<T> response = new ApiResponseWrapper<>();
        response.setStatus(ApiStatus.FAILURE);
        response.setError(error);
        response.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        return response;
    }

    public static <T> ApiResponseWrapper<T> whenFail(CommonBusinessException exception) {
        ApiResponseWrapper<T> response = new ApiResponseWrapper<>();
        response.setStatus(ApiStatus.FAILURE);
        response.setError(exception.getMessage());
        response.setCode(exception.getCode());
        return response;
    }

    public static ApiResponseWrapper<String> fail(Integer code, String error) {
        ApiResponseWrapper<String> response = new ApiResponseWrapper<>();
        response.setStatus(ApiStatus.FAILURE);
        response.setError(error);
        response.setCode(code);
        return response;
    }

    /**
     * 返回自动以异常
     *
     * @param exception 异常
     * @return response
     */
    public static ApiResponseWrapper<String> fail(CommonBusinessException exception) {
        return fail(exception.getCode(), exception.getMessage());
    }

    /**
     * 添加headers信息
     *
     * @param headers 头
     * @return response
     */
    public ApiResponseWrapper<T> withHeaders(Map<String, String> headers) {
        header.putAll(headers);
        return this;
    }

    /**
     * 添加数据来源
     *
     * @param origin 数据来源
     * @return response
     */
    public ApiResponseWrapper<T> origin(String origin) {
        header.put(ORIGIN, origin);
        return this;
    }

    public ApiResponseWrapper<T> fromGateway() {
        header.put(ORIGIN, ResponseOrigin.gateway.name());
        return this;
    }

    public ApiResponseWrapper<T> fromService() {
        header.put(ORIGIN, ResponseOrigin.service.name());
        return this;
    }

    public ApiResponseWrapper<T> fromAdmin() {
        header.put(ORIGIN, ResponseOrigin.admin.name());
        return this;
    }

    public ApiResponseWrapper<T> fromJob() {
        header.put(ORIGIN, ResponseOrigin.job.name());
        return this;
    }

    public boolean ok() {
        return status == ApiStatus.SUCCESS;
    }
}